/**
 * @Title: UserContext.java
 * @Package tech.youmu.ckl.utils
 * Copyright: Copyright (c) 2016
 * Company:成都友木科技有限公司
 * 
 * @author youmu-zh
 * @date 2016年8月2日 下午6:52:10
 * @version V1.0
 */

package com.youmu.shop.context.utils;

import javax.servlet.ServletContext;

import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.context.ContextLoader;

import com.youmu.shop.common.utils.ConfigUtil;
import com.youmu.shop.system.bean.Admin;
import com.youmu.shop.system.vo.UserInfo;

import net.sf.json.JSONObject;


/**
 * 
 * <p>Title:UserContext</p>
 * @author xc
 * @version	v1.0
 * <p>Date:2016年11月24日上午9:48:51</p>
 * <p>Description:TODO</p>
 */
public class UserContext {
    
	/**
	 * 用户存在session中的key
	 */
	private static final String USER_INFO_SESSION = "userInfo";
	
	private static final String ADMIN_INFO_SESSION = "adminInfo";
	
	public static void setUserInfo(UserInfo userInfo){
		SessionContext.setSession(USER_INFO_SESSION, userInfo);
	}
	
	public static void setAdminInfo(Admin admin){
		SessionContext.setSession(ADMIN_INFO_SESSION, admin);
	}
	
	public static UserInfo getUserInfo(){
		return (UserInfo) SessionContext.getSession(USER_INFO_SESSION);
	}
	
	public static Admin getAdminInfo(){
		return (Admin) SessionContext.getSession(ADMIN_INFO_SESSION);
	}

    /**
     *  <p>Author:zh;</p>
     *  <p>Date:2016年10月17日上午11:51:16;</p>
     *	<p>Description: 删除session中的用户;</p>
     */
    public static void removeUser() {
    	SessionContext.removeSession(ADMIN_INFO_SESSION);
    }

    /**
     * 获取servletContext
     */
    public static ServletContext getServletContext() {
        return ContextLoader.getCurrentWebApplicationContext().getServletContext();
    }
    
    /**
     * 获取servletContext 中的属性
     */
    public static Object getAttrInServletContext(String name) {
        return getServletContext().getAttribute(name);
    }
    
    /**
     * 获取servletContext 中的属性
     */
    public static void setAttrInServletContext(String name, Object obj) {
        getServletContext().setAttribute(name, obj);
    }

    /**
     * 获取公众号操作的accessToken
     */
    public static String getAccessToken() {
        String accessToken = null;
        RestTemplate restTemplate = new RestTemplate();
        // 获取本地保存的accessToken
        accessToken = (String) getAttrInServletContext(ConfigUtil.getWeiXinTokenKey());
        // 获取accessToken 的过期时间
        String accessTokenExpire = ConfigUtil.getWeiXinTokenKey() + "-expire";
        Long expireTime = (Long) getAttrInServletContext(accessTokenExpire);
        expireTime = expireTime == null ? 0L : expireTime;
        // 如果过期 或者accessToken不存在重新获取
        if(org.springframework.util.StringUtils.isEmpty(accessToken) || expireTime <= System.currentTimeMillis()){
            String url = ConfigUtil.getWeixinBasicGetAccessToken().replace("APPID", ConfigUtil.getWeixinAppId()).replace("APPSECRET", ConfigUtil.getWeixinAppSecre());
            JSONObject jsonObject = restTemplate.getForObject(url, JSONObject.class);
            accessToken = jsonObject.getString("access_token");
            if(StringUtils.isEmpty(accessToken)){
                throw new RuntimeException("请求执行token出错"+ jsonObject.getInt("errcode") + jsonObject.getString("errmsg"));
            }
            
            // 保存accessToken和过期时间
            setAttrInServletContext(ConfigUtil.getWeiXinTokenKey(),jsonObject.getString("access_token"));
            setAttrInServletContext(accessTokenExpire, Long.valueOf(System.currentTimeMillis() + 7000 * 1000));
        }
        return accessToken;
    }
    
}
